<?php

class FormBuilder_Element_Label extends FormBuilder_Element
{
	const FRONT  = 'front';
	const BEHIND = 'behind';
	const WRAP   = 'wrap';

#------------------------------------------------------------------------------

	public function __construct ($name, $value = null, $position = self::FRONT, $attrs = null)
	{
		$this->elm[] = 'position';
		parent::__construct($name, $value, $attrs);
		$this->setPosition($position);
		$this->label = $this;
	}

#------------------------------------------------------------------------------

	public function __tostring ()
	{
		$formats = array ( self::FRONT => '<label for="%s">%s</label> [ITEM]',
			self::BEHIND => '[ITEM] <label for="%s">%s</label>',
	       		self::WRAP   => '<label for="%s">%s [ITEM]</label>'
		);
		$format = $formats[$this->elm['position']];

		return sprintf($format, $this->elm['name'], $this->elm['value']);
	}

#------------------------------------------------------------------------------

	public function __call ($func, $args)
	{
		$r = parent::__call($func, $args);

		if ($r !== false) {
			return $r;
		}

		if (substr($func, 0, 3) === 'set') {
			$func = strpos($func, 3);
			switch ($func) {

				case 'for':
					return $this->setName($args[0]);
				break;

				case 'text':
					return $this->setValue($args[0]);
				break;

			}
		}

		return false;
	}

#------------------------------------------------------------------------------

	public function setName ($name = null)
	{
		$name = defvalue($name);
		if ($name === false) {
			$error = 'For cannot be empty';

			throw new FormBuilder_Exception($error, 555);
			return false;
		}

		if (!preg_match('/^[a-z][\w\-\][:.=]+/i', $name)) {
			$error = 'For must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").';

			throw new FormBuilder_Exception($error, 209);
			return false;
		}

		if (!FormBuilder_Children::exists($name)) {
			$error = 'The chosen name does not exist within the registery. Cannot use it for the label.';

			throw new FormBuilder_Exception($error, 555);
			return false;

		}

		$this->elm['name'] = $name;
		return $this;
	}

	public function setValue ($value = null)
	{
		parent::setValue($value);

		if (empty($this->elm['value'])) {;
			$this->elm['value'] = ucwords($this->elm['name']);
			return $this;
		}
		
		$this->elm['value'] = html_entity_decode($this->elm['value'], ENT_NOQUOTES, 'UTF-8');
		return $this;
	}

	public function setPosition ($position = self::FRONT)
	{
		$index = array ( self::FRONT, self::BEHIND, self::WRAP );
		$position = defvalue(strtolower($position), self::FRONT);

		if (!in_array($position, $index)) {
			$error = '';

			throw new FormBuilder_Exception($error, 555);
			return false;
		}

		$this->elm['position'] = $position;
		return $this;
	}

#------------------------------------------------------------------------------

	public function addLabel ($text = null, $position = null)
	{}
}

